﻿using DAL.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Site : System.Web.UI.MasterPage
{
    public string SiteRoot { get; set; }
    public UserHelper UserObject { get; set; }

    protected void Page_Init(object sender, EventArgs e)
    {
        if (Session["User"] == null)
        {
            Response.Redirect("login.aspx");
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        SiteRoot = HttpContext.Current.Request.Url.OriginalString.Replace(HttpContext.Current.Request.Url.PathAndQuery, "") + "/";

        if (!IsPostBack)
        {
            UserObject = (UserHelper)Session["User"];

            userFullName.HRef = String.Format("useraccount.aspx?id={0}", UserObject.UserID);
            userFullName.InnerHtml += UserObject.FullName;

            liBanks.Visible = UserObject.Permissions.Contains("ManagmentBanks");
            liCurrency.Visible = UserObject.Permissions.Contains("ManagmentCurrency");
            liFlags.Visible = UserObject.Permissions.Contains("ManagmentFlags");
            liUsers.Visible = UserObject.Permissions.Contains("ManagmentUsers");
            liVessel.Visible = UserObject.Permissions.Contains("ManagmentVessels");
            liVesselType.Visible = UserObject.Permissions.Contains("ManagmentVesselTypes");
        }
    }

    protected void btnLogOut_Click(object sender, EventArgs e)
    {
        Session.Abandon();
        Session.Clear();
        Session.RemoveAll();
        //Request.Cookies.Remove("userAuth");
        var cookie = Request.Cookies["userAuth"];
        if (cookie != null)
        {
            cookie.Expires = DateTime.Now.AddDays(-1d);
            Response.Cookies.Add(cookie);
        }
        Response.Redirect(Request.Url.OriginalString);
    }
}
